using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class HandleFightWindow : MonoBehaviour
{
    //获取预制体
    public GameObject cubePrefab;
    //得到方块阵列的根节点
    public Transform cubeRoot;
    //方块间距
    public float xSpace;
    public float ySpace;
    
    private List<Cubeltem> destroyList = new ();//用于存储可消除的方块
    private Cubeltem[,] cubeArray = new Cubeltem[6, 6];//方块阵列

    private void Start()
    {
        Init();
    }

    public void Init()
    {
        InitCubeData();
    }

    private void InitCubeData()
    {
        int[,] randomArray = null;
        while (true)
        {
            randomArray = GetRandomArray(6, 6);
            if (isValidData(randomArray))//判断是否有重复方块
            {
                break;
            }
        }
        
        //生成一个6x6的方块阵列，每个方块的颜色随机
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                GameObject cube = Instantiate(cubePrefab, cubeRoot);
                //获取脚本
                Cubeltem cubeItem = cube.GetComponent<Cubeltem>();
                cubeItem.Init(i, j, randomArray[i, j]);//设置位置和图片
                cubeItem.name = "Item_" + i + "_" + j;
                cubeItem.transform.localPosition = new Vector3(i * xSpace, j * ySpace,0);
                //设置点击事件
                // cubeItem.GetComponent<Button>().onClick.AddListener(cubeItem.OnItemClicked);

                cubeItem.GetComponent<Button>().onClick.AddListener(() => { OnItemClicked(cubeItem); });
                cubeArray[i, j] = cubeItem;
            }
        }
    }

    public void OnItemClicked(Cubeltem clickItem)
    {
        Debug.Log("Item Clicked" + clickItem.name);
        //检测当前点击的小块是否可以消除
        //可以则消除，否则减少已经拥有的技能点
        if (FindDestroyItems(clickItem))
        {
            
        }
        else
        {
            //减少技能点
        }

    }

    private bool FindDestroyItems(Cubeltem rootItem)
    {
        destroyList.Clear();
        destroyList.Add(rootItem);
        //存放每轮查找到的根节点
        List<Cubeltem> rootList = new List<Cubeltem>();
        rootList.Add(rootItem);
        //开始查找
        return false;
    }

    //校验是否有消除的方块
    private bool isValidData(int[,] array)
    {
        for (int x = 0; x < 6; x++)
        {
            for (int y = 0; y < 6; y++)
            {
                int val = array[x, y];
                int count = 0;
                //上
                if (y <= 4 && val == array[x, y + 1])
                {
                    count += 1;
                }
                //下
                if (y >= 1 && val == array[x, y - 1])
                {
                    count += 1;
                }
                //左
                if (x >= 1 && val == array[x - 1, y])
                {
                    count += 1;
                }
                //右
                if (x <= 4 && val == array[x + 1, y])
                {
                    count += 1;
                }
                //存在一组可消除的，就是有效数据
                if (count >= 2)
                {
                    return true;//存在2个以上相同的，返回true可以消除
                }
            }
        }

        return false;
    }
    
    
    //返回随机阵列数组
    public int[,] GetRandomArray(int width, int height)
    {
        int[,] array = new int[width, height];
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                array[x, y] = Random.Range(0, 5);
            }
        }
        return array;
    }
}
